﻿using System;
using System.Net;
using System.Net.Mail;
using System.Text;
using ProfileManager.Properties;

namespace ProfileManager.Helpers
{
    public class Email
    {
        public static void SendEmail(string subject, string mailMessage)
        {
            try
            {
                if (Network.CheckConnection())
                {
                    MailMessage message = new MailMessage(new MailAddress(ConfigReader.SmtpLogin),
                                                          new MailAddress(ConfigReader.DeveloperMail))
                                              {
                                                  Subject = subject,
                                                  Body = string.Format("{0}{1}{2}", Globals.Host, Resources.HtmlDoubleNewLine, mailMessage.Replace(Environment.NewLine, Resources.HtmlDoubleNewLine)),
                                                  IsBodyHtml = true
                                              };
                    using (message)
                    {
                        SmtpClient client = new SmtpClient(ConfigReader.SmtpServer)
                                                {
                                                    Credentials =
                                                        new NetworkCredential(ConfigReader.SmtpLogin,
                                                                              ConfigReader.SmtpPassword),
                                                    EnableSsl = true
                                                };
                        using (client)
                        {
                            client.Send(message);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex);
            }
        }

        public static void SendEmail(Exception e, string additionalMessage = null)
        {
            try
            {
                StringBuilder logBuilder = new StringBuilder();
                if (!string.IsNullOrEmpty(additionalMessage))
                {
                    logBuilder.AppendLine(additionalMessage);
                }
                
                logBuilder.AppendLine(string.Format("{0} - {1}", DateTime.Now.ToLongTimeString(), e.Message));
                logBuilder.AppendLine(string.Format("TargetSite: {0}", e.TargetSite));
                logBuilder.AppendLine(string.Format("Source: {0}", e.Source));
                logBuilder.AppendLine(string.Format("StackTrace: {0}", e.StackTrace));

                if (Network.CheckConnection())
                {
                    MailMessage message = new MailMessage(new MailAddress(ConfigReader.SmtpLogin),
                                                          new MailAddress(ConfigReader.DeveloperMail))
                    {
                        Subject = string.Format("ProfileManager - {0} ({1})", e.Message, DateTime.Now),
                        Body = string.Format("{0}{1}{2}", Globals.Host, Resources.HtmlDoubleNewLine, logBuilder.ToString().Replace(Environment.NewLine, Resources.HtmlDoubleNewLine)),
                        IsBodyHtml = true
                    };
                    using (message)
                    {
                        SmtpClient client = new SmtpClient(ConfigReader.SmtpServer)
                        {
                            Credentials =
                                new NetworkCredential(ConfigReader.SmtpLogin,
                                                      ConfigReader.SmtpPassword),
                            EnableSsl = true
                        };
                        using (client)
                        {
                            client.Send(message);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
                //Logger.WriteLog(ex);
            }
        }
    }
}